﻿using Microsoft.Win32;
using System.IO;

namespace TgaScanner
{
    internal class ExportFileItemsCommand : BaseCommand
    {
        private readonly MainWindowViewModel viewModel;

        public ExportFileItemsCommand(MainWindowViewModel viewModel)
        {
            this.viewModel = viewModel;
        }

        public override void Execute(object? parameter)
        {
            if (viewModel.FileItems.Count == 0)
            {
                return;
            }

            var saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Text Files | *.txt";

            if (saveFileDialog.ShowDialog() == true)
            {
                using (var streamWriter = new StreamWriter(saveFileDialog.FileName))
                {
                    foreach (var fileItem in viewModel.FileItems)
                    {
                        streamWriter.WriteLine($"{fileItem.FileName}, {fileItem.Width},{fileItem.Height},{fileItem.Channels}");
                    }
                }

                viewModel.StatusText = $"导出完成，导出文件为：{saveFileDialog.FileName}";
            }
        }
    }
}
